package com.base.pig.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.base.pig.entity.WJgtop;
import com.base.pig.entity.WPlan;
import com.base.pig.entity.WZzjg;
import com.base.pig.mapper.WPlanMapper;
import com.base.pig.service.WJgtopService;
import com.base.pig.service.WPlanService;
import com.base.pig.service.WZzjgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  巡检计划服务实现类
 * </p>
 *
 * @author yjh
 * @since 2022-03-21
 */
@Service
public class WPlanServiceImpl extends ServiceImpl<WPlanMapper, WPlan> implements WPlanService {

    @Autowired
    private WZzjgService zzjgService;

    @Autowired
    private WJgtopService jgtopService;

    @Override
    public JSONObject queryOrgTree(){
        JSONArray arry = new  JSONArray();
        QueryWrapper zzjgWrapper = new QueryWrapper();
        zzjgWrapper.eq("lastid","1");
        List<WZzjg> list = zzjgService.list(zzjgWrapper);
        for (WZzjg zzjg : list) {
            JSONObject object = new JSONObject();
            object.put("key", zzjg.getId());
            object.put("type", "depart");
            object.put("level", "2");
            object.put("title", zzjg.getName());
            object.put("state", "open");
            JSONArray arry1 = new  JSONArray();
            QueryWrapper zzjg1Wrapper = new QueryWrapper();
            zzjg1Wrapper.eq("lastid",zzjg.getId().toString());
            List<WZzjg> list1 = zzjgService.list(zzjg1Wrapper);
            for (WZzjg o1 : list1) {
                JSONObject object1 = new JSONObject();
                BigDecimal id1 = o1.getId(); //id
                String name1 = o1.getName(); //名称
                object1.put("key", id1);
                object1.put("type", "depart");
                object1.put("level", "3");
                object1.put("title", name1);
                /*object1.put("state", "closed");*/
                QueryWrapper zzjg2Wrapper = new QueryWrapper();
                zzjg2Wrapper.eq("lastid",id1.toString());
                List<WZzjg> list2 = zzjgService.list(zzjg2Wrapper);
                JSONArray arry2 = new  JSONArray();
                for (WZzjg o2 : list2) {
                    JSONObject object2 = new JSONObject();
                    BigDecimal id2 = o2.getId(); //id
                    String name2 = o2.getName(); //名称
                    object2.put("key", id2);
                    object2.put("level", "4");
                    object2.put("type", "depart");
                    object2.put("title", name2);
                    /*object2.put("state", "closed");*/
                    QueryWrapper ry1Wrapper = new QueryWrapper();
                    ry1Wrapper.eq("departid",id2.toString());
                    List<WJgtop> listp1 = jgtopService.list(ry1Wrapper);
                    JSONArray arry3 = new  JSONArray();
                    for (WJgtop op1 : listp1) {
                        JSONObject objectp1 = new JSONObject();
                        BigDecimal idp1 = op1.getId(); //id
                        String namep1 = op1.getUsername(); //名称
                        String useridp1 = op1.getUserid(); //名称
                        objectp1.put("key", idp1);
                        objectp1.put("userid", useridp1);
                        objectp1.put("level", "5");
                        objectp1.put("type", "person");
                        objectp1.put("isLeaf", "true");
                        objectp1.put("title", namep1);
                        /*object2.put("state", "closed");*/
                        arry3.add(objectp1);
                    }
                    object2.put("children", arry3);
                    arry2.add(object2);
                }
                QueryWrapper ry2Wrapper = new QueryWrapper();
                ry2Wrapper.eq("departid",id1.toString());
                List<WJgtop> listp = jgtopService.list(ry2Wrapper);
                for (WJgtop op : listp) {
                    JSONObject objectp = new JSONObject();
                    BigDecimal idp = op.getId(); //id
                    String namep = op.getUsername(); //名称
                    String useridp = op.getUserid(); //userid
                    objectp.put("key", idp);
                    objectp.put("userid", useridp);
                    objectp.put("level", "4");
                    objectp.put("type", "person");
                    objectp.put("isLeaf", "true");
                    objectp.put("title", namep);
                    /*objectp.put("state", "closed");*/
                    arry2.add(objectp);
                }
                object1.put("children", arry2);
                arry1.add(object1);
            }
            QueryWrapper ryWrapper = new QueryWrapper();
            ryWrapper.eq("departid",zzjg.getId());
            List<WJgtop> listp0 = jgtopService.list(ryWrapper);
            for (WJgtop op : listp0) {
                JSONObject objectp0 = new JSONObject();
                objectp0.put("key", op.getId());
                objectp0.put("userid", op.getUserid());
                objectp0.put("level", "3");
                objectp0.put("type", "person");
                objectp0.put("isLeaf", "true");
                objectp0.put("title", op.getUsername());
                arry1.add(objectp0);
            }
            object.put("children", arry1);
            arry.add(object);
        }
        JSONObject objecthead = new JSONObject();
        objecthead.put("key", "1");
        objecthead.put("type", "depart");
        objecthead.put("level", "1");
        objecthead.put("title", "智慧管网组织架构");
        objecthead.put("state", "open");
        objecthead.put("children", arry);
        return objecthead;
    }

}
